<?php 
namespace app\admin\controller;

/**
 * 权限
 */
class Auth extends Base{
	
	public function index(){
		$page = $this->request->param('page/d',1);
		$limit = $this->request->param('limit/d',20);
		$keyword = $this->request->param('keyword','','trim');
		$parent_id = $this->request->param('parent_id/d',0);
		$where=NULL;
		if( $keyword ){
			$where = $keyword ? " `title` LIKE '%".$keyword."%'" : '1=1';
		}else{
			if($parent_id){
				$where[] = ['auth_class_id','=',$parent_id];
			}
		}
		$totalCount = db('auth')->where($where)-> count();
		$list = db('auth')->where($where)->page($page,$limit)->order(['order_num','id'=>'asc'])->select();
		$data['code'] = 0;
		$data['data'] = $list;
		$data['count'] = $totalCount;
		$data['msg'] = '请求成功';

		if( $this->request->isAjax() ){
			return $data;
		}else{
			$auth_class = db('auth_class')->where('status = 0')->order(['order_num','id'=>'asc'])->select();
			$this->assign('auth_class',$auth_class);
			return $this->fetch();
		}
	}

	public function add(){
		if($this->request-> isPost() || $this->request-> isAjax()){
			$post = input('post.');
			$where = 'm = "'.$post['m'].'" AND c = "'.$post['c'].'" AND a = "'.$post['a'].'"';
			$count = db('auth')->where($where)->count();
			if( $count != 0 ){
				addAdminLog('添加权限，操作失败');
				$data= ['err'=>1,'msg'=>'该权限已经存在，请勿重复添加！'];
			   	return $data;
			}
			$res = db('auth')->insert($post);
			if( $res != 1 ){
				addAdminLog('添加权限，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}else{
				addAdminLog('添加权限，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}
		}else{
			$auth_class = db('auth_class')->where('status = 0')->select();
			$this->assign('auth_class',$auth_class);
			return $this->fetch();
		}
	}

	public function edit(){
		$id = $this->request->param('id','','trim');
		if($this->request-> isPost() || $this->request-> isAjax()){
			$post = input('post.');
			$where = 'm = "'.$post['m'].'" AND c = "'.$post['c'].'" AND a = "'.$post['a'].'"';
			$auth = db('auth')->where($where)->find();
			if( $auth['id'] != $post['id'] ){
				addAdminLog('编辑权限，操作失败');
				$data= ['err'=>1,'msg'=>'该权限已经存在，请勿重复添加！'];
			   	return $data;
			}
			$res = db('auth')->update($post);
			if( $res != 1 ){
				addAdminLog('编辑权限，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}else{
				addAdminLog('编辑权限，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}
		}else{
			$auth_class = db('auth_class')->where('status = 0')->select();
			$info = db('auth')->where("id = ".$id)->find();
			$cList = getControllers('../application/'.$info['m'].'/controller/');
			$aList = getMethods('../application/'.$info['m'].'/controller/'.$info['c']);
			
			$this->assign('auth_class',$auth_class);
			$this->assign('info',$info);
			$this->assign('cList',$cList);
			$this->assign('aList',$aList);
			return $this->fetch();
		}
	}

	public function getControllers(){
		$m = $this->request->param('m','','trim');
		$controllerList = getControllers('../application/'.$m.'/controller/');
		$data= ['err'=>0,'msg'=>'操作成功！','data'=>$controllerList];
		return $data;
	}

	public function getMethods(){
		$c = $this->request->param('c','','trim');
		$m = $this->request->param('m','','trim');
		$res = getMethods('../application/'.$m.'/controller/'.$c);
		if( !is_null($res) ){
			$data= ['err'=>0,'msg'=>'操作成功！','data'=>$res];
			return $data;
		}else{
			$data= ['err'=>1,'msg'=>'无数据！'];
			return $data;
		}
	}
}